package com.exam.teacher.service.Impl;

import com.exam.entity.*;
import com.exam.entity.dto.ScoreDto;
import com.exam.entity.dto.StudentDto;
import com.exam.entity.dto.TeacherDto;
import com.exam.entity.vo.ExamVo;
import com.exam.student.mapper.UserMapper;
import com.exam.teacher.mapper.TeacherMapper;
import com.exam.teacher.service.TeacherService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据id来删除试卷
     * @param examCode
     * @return
     */
    @Transactional
    @Override
    public ApiResult deleteExamById(Long examCode) {

        ApiResult apiResult = new ApiResult();
        int count = teacherMapper.deleteExamById(examCode);

        if (count > 0) {
            apiResult.setCode(200);
            apiResult.setMessage("删除成功");
        } else {
            // 错误，抛出异常
            System.out.println("删除失败");
        }

        return apiResult;
    }

    /**
     * 修改试卷信息
     * @param examManage
     * @return
     */
    @Transactional
    @Override
    public ApiResult updateExam(ExamManage examManage) {

        // 根据id删除试卷
        int countDelete = teacherMapper.deleteExamById(examManage.getExamCode());

        // 拿到被删除的id将数据插入进去
        int countUpdate = teacherMapper.insertExam(examManage);

        ApiResult apiResult = new ApiResult();
        if (countDelete > 0 && countUpdate > 0) {
            apiResult.setCode(200);
            apiResult.setMessage("修改成功");
        } else {
            apiResult.setCode(505);
            apiResult.setMessage("修改失败");
        }
        return apiResult;
    }

    /**
     * 添加考试信息
     * @param examManage
     * @return
     */
    @Transactional
    @Override
    public ApiResult insertExamInfo(ExamManage examManage) {

        examManage.setExamCode(System.currentTimeMillis());

        int countUpdate = teacherMapper.insertExam(examManage);
        ApiResult apiResult = new ApiResult();

        if (countUpdate > 0) {
            apiResult.setCode(200);
            apiResult.setMessage("插入成功");
        } else {
            apiResult.setCode(506);
            apiResult.setMessage("插入失败");
        }

        return apiResult;
    }

    /**
     * 查询所有的试卷id信息
     * @return
     */
    @Override
    public ApiResult getByPaperId() {

        List<PaperManage> paperManage = teacherMapper.getByPaperId(null);
        ApiResult apiResult = new ApiResult();

        if (paperManage != null) {
            apiResult.setCode(200);
            apiResult.setData(paperManage);
            apiResult.setMessage("查询成功");
        } else {
            apiResult.setCode(506);
            apiResult.setMessage("查询失败");
        }

        return apiResult;
    }

    /**
     * 查询所有的题目
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ApiResult findAllAnswers(Integer pageNum, Integer pageSize) {

        // 返回的结果集
        Page<ExamVo> page = null;
        PageHelper.startPage(pageNum, pageSize);
        // 查询所有的题
        page = teacherMapper.selectAllQuestion();

        for (ExamVo examVo : page) {
            examVo.setLevel(examVo.getLevel() + "颗星");
        }

        ApiResult apiResult = new ApiResult();

        if (page == null) {
            // 抛出异常
            apiResult.setCode(506);
            apiResult.setMessage("查询失败，此试卷无试题");
        } else {
            apiResult.setCode(200);
            apiResult.setMessage("查询成功");
            apiResult.setTotal(page.getTotal());
            apiResult.setSize(page.getPageSize());
            apiResult.setCurrent(pageNum);
            apiResult.setData(page);
        }

        return apiResult;
    }

    /**
     * 添加选择题并且添加试题管理
     * @param multiQuestion
     * @return
     */
    @Transactional
    @Override
    public ApiResult addMultiQuestion(MultiQuestion multiQuestion) {

        // 添加选择题
        multiQuestion.setScore(2);
        multiQuestion.setQuestionType(1);
        int countMulti = teacherMapper.addMultiQuestion(multiQuestion);

        // 添加试卷管理
        PaperManage paperManage = new PaperManage();
        paperManage.setPaperId(multiQuestion.getPaperId());
        paperManage.setQuestionId(multiQuestion.getQuestionId());
        paperManage.setQuestionType(multiQuestion.getQuestionType());
        int countExam = teacherMapper.addExamManage(paperManage);

        // 数据响应返回
        ApiResult apiResult = new ApiResult();
        if (countMulti > 0 && countExam > 0){
            apiResult.setCode(200);
            apiResult.setMessage("插入成功");
        }else{
            apiResult.setCode(506);
            apiResult.setMessage("插入失败");
        }

        return apiResult;
    }

    /**
     * 添加填空题并且添加试题管理
     * @param fillQuestion
     * @return
     */
    @Transactional
    @Override
    public ApiResult addFillQuestion(FillQuestion fillQuestion) {

        // 添加填空题
        fillQuestion.setScore(2);
        fillQuestion.setQuestionType(2);
        int countFill = teacherMapper.addFillQuestion(fillQuestion);

        // 添加试卷管理
        PaperManage paperManage = new PaperManage();
        paperManage.setPaperId(fillQuestion.getPaperId());
        paperManage.setQuestionId(fillQuestion.getQuestionId());
        paperManage.setQuestionType(fillQuestion.getQuestionType());
        int countExam = teacherMapper.addExamManage(paperManage);

        // 数据响应返回
        ApiResult apiResult = new ApiResult();
        if (countFill > 0 && countExam > 0){
            apiResult.setCode(200);
            apiResult.setMessage("插入成功");
        }else{
            apiResult.setCode(506);
            apiResult.setMessage("插入失败");
            System.out.println("插入失败");
        }

        return apiResult;
    }

    /**
     * 添加判断题并且添加试题管理
     * @param judgeQuestion
     * @return
     */
    @Override
    public ApiResult addJudgeQuestion(JudgeQuestion judgeQuestion) {

        // 添加判断题
        judgeQuestion.setScore(2);
        judgeQuestion.setQuestionType(3);
        int countJudge = teacherMapper.addJudgeQuestion(judgeQuestion);

        // 添加试卷管理
        PaperManage paperManage = new PaperManage();
        paperManage.setPaperId(judgeQuestion.getPaperId());
        paperManage.setQuestionId(judgeQuestion.getQuestionId());
        paperManage.setQuestionType(judgeQuestion.getQuestionType());
        int countExam = teacherMapper.addExamManage(paperManage);

        // 数据结果返回
        ApiResult apiResult = new ApiResult();
        if (countJudge > 0 && countExam > 0){
            apiResult.setCode(200);
            apiResult.setMessage("插入成功");
        }else{
            apiResult.setCode(506);
            apiResult.setMessage("插入失败");
            System.out.println("插入失败");
        }

        return apiResult;
    }

    /**
     * 查询所有的学生信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ApiResult findAllStudent(Integer pageNum, Integer pageSize) {

        // 返回的结果集
        Page<Student> page = null;
        PageHelper.startPage(pageNum, pageSize);
        // 查询所有的学生
        page = teacherMapper.findAllStudent();

        ApiResult apiResult = new ApiResult();

        if (page == null) {
            // 抛出异常
            apiResult.setCode(506);
            apiResult.setMessage("查询失败");
        } else {
            apiResult.setCode(200);
            apiResult.setMessage("查询成功");
            apiResult.setTotal(page.getTotal());
            apiResult.setSize(page.getPageSize());
            apiResult.setCurrent(pageNum);
            apiResult.setData(page);
        }

        return apiResult;
    }

    /**
     * 根据学生id查询该学生所有的分数
     * @param scoreDto
     * @return
     */
    @Override
    public ApiResult findByStudentIdScore(ScoreDto scoreDto) {

        List<Score> studentList = teacherMapper.findByStudentIdScore(scoreDto);

        ApiResult apiResult = new ApiResult();
        if (studentList != null && studentList.size() > 0) {
            apiResult.setCode(200);
            apiResult.setMessage("查询成功");
            apiResult.setData(studentList);
        } else {
            // 抛出异常
            apiResult.setCode(506);
            apiResult.setMessage("查询失败");
        }

        return apiResult;
    }

    /**
     * 学生管理编辑回显学生数据
     * @param studentDto
     * @return
     */
    @Override
    public ApiResult findByIdStudent(StudentDto studentDto) {

        Student student = teacherMapper.findByIdStudent(studentDto);

        ApiResult apiResult = new ApiResult();
        if (student != null) {
            apiResult.setCode(200);
            apiResult.setMessage("查询成功");
            apiResult.setData(student);
        } else {
            // 抛出异常
            apiResult.setCode(506);
            apiResult.setMessage("查询失败");
        }

        return apiResult;
    }

    /**
     * 学生管理编辑插入学生数据
     * @param studentDto
     * @return
     */
    @Transactional
    @Override
    public ApiResult updateStudent(StudentDto studentDto) {

        // 根据id删除用户信息
        int countDelete = teacherMapper.deleteByIdStudent(studentDto.getStudentId());

        // 插入数据
        int countInsert = teacherMapper.insertStudent(studentDto);

        // 返回数据
        ApiResult apiResult = new ApiResult();
        if (countDelete > 0 && countInsert > 0) {
            apiResult.setCode(200);
            apiResult.setMessage("修改成功");
        } else {
            apiResult.setCode(506);
            apiResult.setMessage("修改失败");
        }

        return apiResult;
    }

    /**
     * 根据id删除学生信息
     * @param studentId
     * @return
     */
    @Transactional
    @Override
    public ApiResult deleteByIdStudent(Long studentId) {

        // 根据id删除用户信息
        int countDelete = teacherMapper.deleteByIdStudent(studentId);

        // 返回数据
        ApiResult apiResult = new ApiResult();
        if (countDelete > 0) {
            apiResult.setCode(200);
            apiResult.setMessage("删除成功");
        } else {
            apiResult.setCode(506);
            apiResult.setMessage("删除失败");
        }

        return apiResult;
    }

    /**
     * 添加学生
     * @param studentDto
     * @return
     */
    @Override
    public ApiResult insertStudent(StudentDto studentDto) {

        studentDto.setStudentId(System.currentTimeMillis());
        int countInsert = teacherMapper.insertStudent(studentDto);

        // 返回数据
        ApiResult apiResult = new ApiResult();
        if (countInsert > 0) {
            apiResult.setCode(200);
            apiResult.setMessage("添加成功");
        } else {
            apiResult.setCode(506);
            apiResult.setMessage("添加失败");
        }

        return apiResult;
    }

    /**
     * 查找所有的教师分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ApiResult findAllTeacher(Integer pageNum, Integer pageSize) {

        // 返回的结果集
        Page<Teacher> page = null;
        PageHelper.startPage(pageNum, pageSize);
        // 查询所有的教师
        page = teacherMapper.findAllTeacher();

        ApiResult apiResult = new ApiResult();

        if (page == null) {
            // 抛出异常
            apiResult.setCode(506);
            apiResult.setMessage("查询失败");
        } else {
            apiResult.setCode(200);
            apiResult.setMessage("查询成功");
            apiResult.setTotal(page.getTotal());
            apiResult.setSize(page.getPageSize());
            apiResult.setCurrent(pageNum);
            apiResult.setData(page);
        }

        return apiResult;
    }

    /**
     * 教师管理编辑回显学生数据
     * @param teacherDto
     * @return
     */
    @Override
    public ApiResult findByIdTeacher(TeacherDto teacherDto) {
        Teacher teacher = userMapper.findTeacherById(teacherDto.getTeacherId());

        ApiResult apiResult = new ApiResult();
        if (teacher != null) {
            apiResult.setCode(200);
            apiResult.setMessage("查询成功");
            apiResult.setData(teacher);
        } else {
            // 抛出异常
            apiResult.setCode(506);
            apiResult.setMessage("查询失败");
        }

        return apiResult;
    }

    /**
     * 教师管理编辑插入教师数据
     * @param teacherDto
     * @return
     */
    @Transactional
    @Override
    public ApiResult updateTeacher(TeacherDto teacherDto) {

        // 根据id删除教师信息
        int countDelete = teacherMapper.deleteByIdTeacher(teacherDto.getTeacherId());

        // 插入数据
        int countInsert = teacherMapper.insertTeacher(teacherDto);

        // 返回数据
        ApiResult apiResult = new ApiResult();
        if (countDelete > 0 && countInsert > 0) {
            apiResult.setCode(200);
            apiResult.setMessage("修改成功");
        } else {
            apiResult.setCode(506);
            apiResult.setMessage("修改失败");
        }

        return apiResult;
    }

    /**
     * 根据id删除教师信息
     * @param teacherId
     * @return
     */
    @Override
    public ApiResult deleteByIdTeacher(Long teacherId) {

        // 根据id删除教师信息
        int countDelete = teacherMapper.deleteByIdTeacher(teacherId);

        // 返回数据
        ApiResult apiResult = new ApiResult();
        if (countDelete > 0) {
            apiResult.setCode(200);
            apiResult.setMessage("删除成功");
        } else {
            apiResult.setCode(506);
            apiResult.setMessage("删除失败");
        }

        return apiResult;
    }

    /**
     * 添加教师
     * @param teacherDto
     * @return
     */
    @Override
    public ApiResult insertTeacher(TeacherDto teacherDto) {

        teacherDto.setTeacherId(System.currentTimeMillis());
        int countInsert = teacherMapper.insertTeacher(teacherDto);

        // 返回数据
        ApiResult apiResult = new ApiResult();
        if (countInsert > 0) {
            apiResult.setCode(200);
            apiResult.setMessage("添加成功");
        } else {
            apiResult.setCode(506);
            apiResult.setMessage("添加失败");
        }

        return apiResult;
    }

}



